﻿#include "MenuButton.h"
#include "components/FWidgets/FMenu/FMenu.h"
#include "stores/StoreFilePath.h"

#include "./SubViews/BackgroundImageDialog/BackgroundImageDialog.h"
#include "./SubViews/BugReportDialog/BugReportDialog.h"

#include <QProcess>


MenuButton::MenuButton(QWidget *parent)
    : QPushButton{parent}
{
    setIcon(QIcon(":/imgs/menu.png"));

    FMenu* menu = new FMenu(this);

    QAction* actShowGuide = new QAction(QIcon(":/imgs/guide.png"), tr("User Guide"), menu);
    connect(actShowGuide, &QAction::triggered, this, &MenuButton::showGuideTriggered);
    actShowGuide->setShortcut(QKeySequence("Ctrl+Alt+G"));

    QAction* actBackgroundImage = new QAction(QIcon(":/imgs/theme.png"), tr("Background Image"), menu);
    connect(actBackgroundImage, &QAction::triggered, this, &MenuButton::onActBackgroundImage);

    QAction* actBugReport = new QAction(QIcon(":/imgs/bug.png"), tr("Bug Report"), menu);
    connect(actBugReport, &QAction::triggered, this, &MenuButton::onActBugReport);

    QAction* actCheckUpdate = new QAction(QIcon(":imgs/update.png"), tr("Check Update"), menu);
    connect(actCheckUpdate, &QAction::triggered, this, &MenuButton::onActCheckUpdate);

    menu->addAction(actShowGuide);
    menu->addAction(actBackgroundImage);
    menu->addSeparator();
    menu->addAction(actBugReport);
    menu->addAction(actCheckUpdate);

    setMenu(menu);
}

void MenuButton::onActBackgroundImage()
{
    BackgroundImageDialog dlg(this);
    int ret = dlg.exec();
    if (ret == FDialog::Accepted) {
        emit backgroundImageChanged();
    }
}

void MenuButton::onActBugReport()
{
    BugReportDialog(this).exec();
}

void MenuButton::onActCheckUpdate()
{
    QProcess::startDetached(StoreFilePath::updateAppFilePath());
}
